| Conditions | 1 |
| Paths | 4 |
| Total Lines | 144 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* |
||
| 23 | app.factory('ApiService', function($http, $q){ |
||
| 24 | var ApiService = function(http, endpoint) { |
||
| 25 | this.endpoint = endpoint; |
||
| 26 | this.baseUrl = OC.generateUrl('/apps/deck/' + endpoint); |
||
|
|
|||
| 27 | this.http = http; |
||
| 28 | this.q = $q; |
||
| 29 | this.data = {}; |
||
| 30 | this.id = null; |
||
| 31 | this.sorted = []; |
||
| 32 | }; |
||
| 33 | |||
| 34 | ApiService.prototype.fetchAll = function(){ |
||
| 35 | var deferred = $q.defer(); |
||
| 36 | var self = this; |
||
| 37 | $http.get(this.baseUrl).then(function (response) { |
||
| 38 | var objects = response.data; |
||
| 39 | objects.forEach(function (obj) { |
||
| 40 | self.data[obj.id] = obj; |
||
| 41 | }); |
||
| 42 | deferred.resolve(self.data); |
||
| 43 | }, function (error) { |
||
| 44 | deferred.reject('Fetching ' + self.endpoint + ' failed'); |
||
| 45 | }); |
||
| 46 | return deferred.promise; |
||
| 47 | }; |
||
| 48 | |||
| 49 | ApiService.prototype.fetchOne = function (id) { |
||
| 50 | |||
| 51 | this.id = id; |
||
| 52 | var deferred = $q.defer(); |
||
| 53 | |||
| 54 | if(id===undefined) { |
||
| 55 | return deferred.promise; |
||
| 56 | } |
||
| 57 | |||
| 58 | var self = this; |
||
| 59 | $http.get(this.baseUrl + '/' + id).then(function (response) { |
||
| 60 | data = response.data; |
||
| 61 | if(self.data[data.id]===undefined) { |
||
| 62 | self.data[data.id] = response.data; |
||
| 63 | } |
||
| 64 | $.each(response.data, function(key, value) { |
||
| 65 | self.data[data.id][key] = value; |
||
| 66 | }); |
||
| 67 | deferred.resolve(response.data); |
||
| 68 | |||
| 69 | }, function (error) { |
||
| 70 | deferred.reject('Fetching ' + self.endpoint + ' failed'); |
||
| 71 | }); |
||
| 72 | return deferred.promise; |
||
| 73 | }; |
||
| 74 | |||
| 75 | ApiService.prototype.create = function (entity) { |
||
| 76 | var deferred = $q.defer(); |
||
| 77 | var self = this; |
||
| 78 | $http.post(this.baseUrl, entity).then(function (response) { |
||
| 79 | self.add(response.data); |
||
| 80 | deferred.resolve(response.data); |
||
| 81 | }, function (error) { |
||
| 82 | deferred.reject('Fetching' + self.endpoint + ' failed'); |
||
| 83 | }); |
||
| 84 | return deferred.promise; |
||
| 85 | }; |
||
| 86 | |||
| 87 | ApiService.prototype.update = function (entity) { |
||
| 88 | var deferred = $q.defer(); |
||
| 89 | var self = this; |
||
| 90 | $http.put(this.baseUrl + '/' + entity.id, entity).then(function (response) { |
||
| 91 | self.add(response.data); |
||
| 92 | deferred.resolve(response.data); |
||
| 93 | }, function (error) { |
||
| 94 | deferred.reject('Updating ' + self.endpoint + ' failed'); |
||
| 95 | }); |
||
| 96 | return deferred.promise; |
||
| 97 | |||
| 98 | }; |
||
| 99 | |||
| 100 | ApiService.prototype.delete = function (id) { |
||
| 101 | var deferred = $q.defer(); |
||
| 102 | var self = this; |
||
| 103 | |||
| 104 | $http.delete(this.baseUrl + '/' + id).then(function (response) { |
||
| 105 | self.remove(id); |
||
| 106 | deferred.resolve(response.data); |
||
| 107 | |||
| 108 | }, function (error) { |
||
| 109 | deferred.reject('Deleting ' + self.endpoint + ' failed'); |
||
| 110 | }); |
||
| 111 | return deferred.promise; |
||
| 112 | |||
| 113 | }; |
||
| 114 | |||
| 115 | |||
| 116 | // methods for managing data |
||
| 117 | ApiService.prototype.clear = function() { |
||
| 118 | this.data = {}; |
||
| 119 | }; |
||
| 120 | ApiService.prototype.add = function (entity) { |
||
| 121 | var element = this.data[entity.id]; |
||
| 122 | if(element===undefined) { |
||
| 123 | this.data[entity.id] = entity; |
||
| 124 | } else { |
||
| 125 | Object.keys(entity).forEach(function (key) { |
||
| 126 | element[key] = entity[key]; |
||
| 127 | if(element[key]!==null) |
||
| 128 | element[key].status = {}; |
||
| 129 | }); |
||
| 130 | } |
||
| 131 | }; |
||
| 132 | ApiService.prototype.remove = function(id) { |
||
| 133 | if (this.data[id] !== undefined) { |
||
| 134 | delete this.data[id]; |
||
| 135 | } |
||
| 136 | }; |
||
| 137 | ApiService.prototype.addAll = function (entities) { |
||
| 138 | var self = this; |
||
| 139 | angular.forEach(entities, function(entity) { |
||
| 140 | self.add(entity); |
||
| 141 | }); |
||
| 142 | }; |
||
| 143 | |||
| 144 | ApiService.prototype.getCurrent = function () { |
||
| 145 | return this.data[this.id]; |
||
| 146 | }; |
||
| 147 | |||
| 148 | ApiService.prototype.getData = function() { |
||
| 149 | return $.map(this.data, function(value, index) { |
||
| 150 | return [value]; |
||
| 151 | }); |
||
| 152 | }; |
||
| 153 | |||
| 154 | ApiService.prototype.getAll = function () { |
||
| 155 | return this.data; |
||
| 156 | }; |
||
| 157 | |||
| 158 | ApiService.prototype.getName = function() { |
||
| 159 | var funcNameRegex = /function (.{1,})\(/; |
||
| 160 | var results = (funcNameRegex).exec((this).constructor.toString()); |
||
| 161 | return (results && results.length > 1) ? results[1] : ""; |
||
| 162 | }; |
||
| 163 | |||
| 164 | return ApiService; |
||
| 165 | |||
| 166 | }); |
||
| 167 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.